import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import org.rsbot.script.GEItemInfo;

public class MMerchantEXTREME {

	static int startPrice;

	static String read;
	static File file = new File("Merchant.txt");;

	static BufferedWriter out = null;
	static BufferedReader in = null;

	static String[] temp = null;
	static int current = 1;
	static int price = 0;

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		try {
			boolean success = file.createNewFile();

			if (success) {
				writeFile();
			} else {
				Calendar now = Calendar.getInstance();
				int day = (now.get(Calendar.DAY_OF_MONTH) - 1);

				in = new BufferedReader(new FileReader(file));
				while ((read = in.readLine()) != null) {
					temp = read.split("\\+");
					if (Integer.parseInt(temp[1]) != day) {
						readLine(1);
						readLine(2);
					}
				}
			}
		} catch (IOException e) {
			System.out.println("There was a problem: " + e);
		} finally {
			in.close();
		}

	}

	public static String loadID() {
		try {
			URL url = new URL("http://www.rsbot.org/vb/showthread.php?t=151988");
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream()));

			String line;
			// <div class="subsectionHeader">
			// <font color="RoyalBlue"><font size="6">BTekksCrabs
			int i = 0;
			while ((line = reader.readLine()) != null) {
				if (line.equals("<font color=" + '"' + "RoyalBlue" + '"'
						+ ">" + "<font size=" + '"' + "6" + '"' + ">BTekksCrabs")) {
					i++;
					return line;
				}
				if (i == 1) {
					return line;
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	public static GEItemInfo loadItemInfo(final int itemID) {
		int minPrice = 0;
		int maxPrice = 0;
		int marketPrice = 0;
		String changeSeven = "";
		String changeThirty = "";

		try {
			final URL url = new URL(
					"http://itemdb-rs.runescape.com/viewitem.ws?obj=" + itemID);
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream()));

			String line;

			while ((line = reader.readLine()) != null) {
				if (line.contains("Minimum price:")) {
					line = line.replace("<b>Minimum price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						minPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						minPrice = Integer.parseInt(line) * 100000;
					} else {
						minPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("Market price:")) {
					line = line.replace("<b>Market price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						marketPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						marketPrice = Integer.parseInt(line) * 100000;
					} else {
						marketPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("Maximum price:")) {
					line = line.replace("<b>Maximum price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						maxPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						maxPrice = Integer.parseInt(line) * 100000;
					} else {
						maxPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("7 Days:")) {
					// <b>7 Days:</b> <span class="rise">+4.3%</span>
					line = line.replace("<b>7 Days:</b> <span class=\"stay\">",
							"");
					line = line.replace("<b>7 Days:</b> <span class=\"stay\">",
							"");
					line = line.replace("</span>", "");
					line = line.trim();
					changeSeven = line;
				}

				if (line.contains("30 Days:")) {
					// <b>7 Days:</b> <span class="rise">+4.3%</span>
					line = line.replace(
							"<b>30 Days:</b> <span class=\"stay\">", "");
					line = line.replace(
							"<b>30 Days:</b> <span class=\"stay\">", "");
					line = line.replace("</span>", "");
					line = line.trim();
					changeThirty = line;
				}
			}
		} catch (final Exception e) {
		}

		return new GEItemInfo(itemID, minPrice, maxPrice, marketPrice,
				changeSeven, changeThirty);
	}

	// Creates the file
	public static void writeFile() {
		try {
			Calendar now = Calendar.getInstance();
			int month = now.get(Calendar.MONTH);
			int day = now.get(Calendar.DAY_OF_MONTH);

			GEItemInfo money = loadItemInfo(10476);
			int price = money.getMarketPrice();
			out = new BufferedWriter(new FileWriter(file, true));
			out.append(Integer.toString(price) + "+" + day + "+" + (month + 1));
			System.out.println(Integer.toString(price) + "+" + day + "+"
					+ (month + 1));
			out.newLine();

			out.close();

		} catch (IOException e) {
			System.out.println("There was a problem:" + e);
		}
	}

	/**
	 * This utility method returns a past date before number of days.
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDateBeforeDays(int days) {
		long backDateMS = System.currentTimeMillis() - ((long) days) * 24 * 60
				* 60 * 1000;
		Date backDate = new Date();
		backDate.setTime(backDateMS);
		return backDate;
	}

	public static int readLine(int line) {
		try {
			in = new BufferedReader(new FileReader(file));
			while ((read = in.readLine()) != null) {
				if (current == line) {
					temp = read.split("\\+");
					splitIt(temp);
					break;
				}
				current++;
			}
			price = Integer.parseInt(temp[0]);
		} catch (IOException e) {
			System.out.println("There was a problem:" + e);
		}
		return current;
	}

	// Reades the file
	public static void readFile() {
		try {
			in = new BufferedReader(new FileReader(file));
			while ((read = in.readLine()) != null) {
				temp = read.split("\\+");
				splitIt(temp);

				in.close();
			}
		} catch (IOException e) {
			System.out.println("There was a problem:" + e);
		}
	}

	public static String day() {
		switch (current) {
		case 1:
			return "First";
		case 2:
			return "Second";
		case 3:
			return "Third";
		case 4:
			return "Fourth";
		case 5:
			return "Fifth";
		case 6:
			return "Sixth";
		case 7:
			return "Seventh";
		case 8:
			return "Eigth";
		case 9:
			return "Nineth";
		case 10:
			return "Tenth";
		}

		return "First day info:";
	}

	public static void splitIt(String[] s) {

		System.out.println("The " + day() + " info:");
		System.out.println("Start price is/was: " + s[0]);
		System.out.println("Start date is/was: " + s[1] + "-" + s[2]);
		System.out.println("Change is: " + (Integer.parseInt(s[0]) - price));
		System.out.println("------------");
	}
}
